<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>常见事件</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            border: 1px solid black;
            margin-left: 100px;
        }
    </style>
    <script>
        //页面加载完毕事件,通常应当在body加载完所有标签之后才能使用document.getElement,如果想要将script写在前面,应当使用window.onload = function(){ ... }来等待页面加载完成才执行函数体
        window.onload = function () {

            document.getElementById("username").onfocus = function () {
                this.style.backgroundColor = "red";
            }

            document.getElementById("username").onblur = function () {
                this.style.backgroundColor = "green";
            }

            document.getElementById("username").onkeydown = function (event) {
                console.log(event.key);
                console.log(event.keyCode);
            }

            document.getElementById("myDiv").onmouseover = function () {
                this.style.backgroundColor = "blue";
            }

            document.getElementById("myDiv").onmouseout = function () {
                this.style.backgroundColor = "yellow";
            }

            //提交时判断，只有用户名为aa可以提交，否则不能提交
            document.getElementById("myForm").onsubmit = function () {
                //alert("我要提交");
                //获取表单中的username
                let username = document.getElementById("username2").value;

                //判断是否为aa
                return username == "aa";//当返回值为true时提交成功,为false时提交失败
            }
        }
    </script>
</head>
<body>
用户名:<input type="text" id="username">
<br>
<br>
<div id="myDiv">

</div>
<br>
<br>

<form action="#" id="myForm">
    用户名:<input type="text" name="username" id="username2"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit">
</form>
</body>
</html>
